/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.thread;

import java.util.concurrent.TimeUnit;

/**
 * <p>编写多线程程序模拟死锁效果
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class T3 {
    final Object locka = new Object();
    final Object lockb = new Object();

    public void m1() {
        String tn = Thread.currentThread().getName();
        System.out.printf("%s启动等待%n", tn);
        synchronized (locka) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            synchronized (lockb) {
            }
        }
        System.out.printf("%s结束%n", tn);
    }

    public void m2() {
        String tn = Thread.currentThread().getName();
        System.out.printf("%s启动等待%n", tn);
        synchronized (lockb) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            synchronized (locka) {
            }
        }
        System.out.printf("%s结束%n", tn);
    }

    public static void main(String[] args) {
        var t = new T3();
        new Thread(t::m1, "A").start();
        new Thread(t::m2, "B").start();
    }
}
